﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Baopx.BackEnd.ManageNews
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        protected void BindData()
        {
            grdNews.DataSource = A.Controller.NewsController.GetAll();
            grdNews.DataBind();
        }

        protected void ItemCommand(object source, DataGridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "cmdAddNew":
//                    N newsCategory = new NewsCategory();
//                    newsCategory.Name = ((TextBox)e.Item.FindControl("txtName")).Text;
//                    newsCategory.OrderDisplay = Convert.ToInt32(((TextBox)e.Item.FindControl("txtOrderDisplay")).Text);
//                    newsCategory.IsActive = true;
//                    A.Controller.NewsCategoryController.Insert(newsCategory);
                    break;
                case "cmdUpdate":
//                    NewsCategory newsCategoryUpdate =
//                        A.Controller.NewsCategoryController.GetElementById(Convert.ToInt32(e.CommandArgument));
//                    newsCategoryUpdate.Name = ((TextBox)e.Item.FindControl("txtName")).Text;
//                    newsCategoryUpdate.OrderDisplay = Convert.ToInt32(((TextBox)e.Item.FindControl("txtOrderDisplay")).Text);
//                    newsCategoryUpdate.IsActive = ((CheckBox)e.Item.FindControl("chkActive")).Checked;
//                    A.Controller.NewsCategoryController.Update();
                    break;
                case "cmdDelete":
//                    NewsCategory newsCategoryDelete =
//                        A.Controller.NewsCategoryController.GetElementById(Convert.ToInt32(e.CommandArgument));
//                    A.Controller.NewsCategoryController.Delete(newsCategoryDelete);
                    break;
            }
            BindData();
        }

        protected void ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var cboCategory = (DropDownList) e.Item.FindControl("cboCategory");
                cboCategory.DataSource = A.Controller.NewsCategoryController.GetAllCategorySub().Where(i=>i.IsActive);
                cboCategory.DataTextField = "Name";
                cboCategory.DataValueField = "Id";
                cboCategory.DataBind();
            }
            if (e.Item.ItemType == ListItemType.Footer)
            {
                var cboCategory = (DropDownList)e.Item.FindControl("cboCategory");
                cboCategory.DataSource = A.Controller.NewsCategoryController.GetAllCategorySub().Where(i => i.IsActive);
                cboCategory.DataTextField = "Name";
                cboCategory.DataValueField = "Id";
                cboCategory.DataBind();
            }
        }
    }
}